package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.enums.*;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;


@Cmd(code = GameProtocol.S_CROSS_PARTY_JOIN_RESULT, desc = "")
public class CrossJoinPartyResultCmd extends AbstractCommand {

    private String reward = "";
    private int pos = 0;
    private String giftTypeAccept = "";

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BanquetProto.CrossJoinPartyRespMsg req = BanquetProto.CrossJoinPartyRespMsg.parseFrom(packet.getBytes());
        ret = req.getRet();
        giftTypeAccept = req.getGiftTypeAccept();
        int giftId = req.getGiftId();
        String consume = req.getConsume();
        //恢复一下
        if (!StringUtils.isNullOrEmpty(consume)) {
            player.getModule(PreReduceModule.class).restorePreReduce(consume);
        }

        if (ret == 0) {
            long popularity = req.getPopularity();
            long integral = req.getIntegral();
            pos = req.getPos();
            reward = BanquetConfigMgr.getJoinReward(pos, giftId);
            if (!StringUtils.isNullOrEmpty(consume)) {
                if (!player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(consume),
                        eLogMoneyType.Banquet, eLogMoneyType.BanquetJoinConsume)) {
                    ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                    return;
                }
            }
            if (!"".equals(reward)) {
                try {
                    player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Banquet, eLogMoneyType.BanquetJoinReward);
                } catch (Exception e) {
                    log.error("user {} addReward {} error", player.getUserId(), reward);
                }
            }
            //加一下参加宴会次数
            BanquetMgr.addBanquetJoinTimes(player);
            CrossBanquetMgr.addBanquetPopularityAndIntegral(player,popularity,integral,eLogMoneyType.BanquetJoinReward.getValue());
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        BanquetProto.JoinBanquetResp.Builder respMsg = BanquetProto.JoinBanquetResp.newBuilder();
        respMsg.setRet(ret);
        respMsg.setReward(reward);
        respMsg.setPos(pos);
        respMsg.setGiftTypeAccept(giftTypeAccept);
        player.sendPacket(Protocol.U_BANQUET_SERVER_JOIN, respMsg);
    }
}
